﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AdventureAuthor.Utils;

namespace AdventureAuthor.Scripts.UI
{
	/// <summary>
	/// Ask a question which will be answered by an enum value.
	/// </summary>
    public partial class EnumQuestionPanel : ParameterPanel
    {
    	/// <summary>
    	/// The type of enum to provide possible answers from.
    	/// </summary>
    	private Type enumType;
    	
        
    	/// <summary>
    	/// Create a question panel which will be answered by an enum value.
    	/// </summary>
    	/// <param name="question">The question to ask the user</param>
        /// <param name="enumType">The enum the user will answer from</param>
        public EnumQuestionPanel(string question, Type enumType)
        {
        	if (!enumType.IsEnum) {
        		throw new ArgumentException("Tried to create an EnumQuestionPanel with a type that was not an enum (type was " +
        		                            enumType.GetType().ToString() + ").");
        	}
            InitializeComponent();
            QuestionLabel.Text = question;  
            this.enumType = enumType;
            
            string[] enumNames = BindingFlags.GetNames(enumType);            
            AnswerBox.ItemsSource = enumNames;
        }
        
        
    	/// <summary>
    	/// Create a question panel which will be answered by an enum value.
    	/// </summary>
    	/// <param name="question">The question to ask the user</param>
        /// <param name="enumType">The enum the user will answer from</param>
        /// <param name="defaultValue">The default value of this answer on loading the window</param>
        public EnumQuestionPanel(string question, Type enumType, string defaultValue) : this(question,enumType)
        {
        	if (!AnswerBox.Items.Contains(defaultValue)) {
        		throw new ArgumentException("Couldn't find " + defaultValue + " in enumeration " + enumType.Name + ".");
        	}        	
        	AnswerBox.SelectedItem = defaultValue;
        }
        
        
        /// <summary>
        /// Returns an object representing an answer to the question posed by this panel - 
        /// the type of object depends on the type of question.
        /// </summary>
        public override object Answer
        {
			get {   
        		return Enum.Parse(enumType,(string)AnswerBox.SelectedItem,true);
        	}
		}
    }
}
